package com.glory.hikvisionmobile.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.LotScrapListAdapter;
import com.glory.hikvisionmobile.adapter.TrackOutHoldListAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.TrackOut.TrackOutLoadBean;
import com.glory.hikvisionmobile.bean.TrackOut.TrackOutSubmitBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class  Work_TrackOutActivity extends BaseActivity {


    @BindView(R.id.tvLot)
    TextView tvLot;
    @BindView(R.id.tvMainWork)
    TextView tvMainWork;
    @BindView(R.id.tvItemWork)
    TextView tvItemWork;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.tvCurrentStep)
    TextView tvCurrentStep;
    @BindView(R.id.etRemarks)
    FreeUI_ClearEditText etRemarks;
    @BindView(R.id.bottom_tv1)
    TextView bottomTv1;
    @BindView(R.id.bottom_tv2)
    TextView bottomTv2;
    @BindView(R.id.bottom_tv3)
    TextView bottomTv3;
    @BindView(R.id.bottom_tv4)
    TextView bottomTv4;
    @BindView(R.id.bottom_btn_layout)
    LinearLayout bottomBtnLayout;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvPartDesc)
    TextView tvPartDesc;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.rvLotHoldInfo)
    RecyclerView rvLotHoldInfo;
    @BindView(R.id.tvPartName)
    TextView tvPartName;

    private String lotType;
    private String lotNum;
    private String edcState;
    private TrackOutLoadBean trackOutLoadBean;
    private TrackOutHoldListAdapter trackOutHoldListAdapter;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_track_out;
    }

    @Override
    protected void initView() {
        initListTitle();
        Bundle bundle = getIntent().getExtras();
        lotType = bundle.getString("lotType");
        lotNum = bundle.getString("lotNum");
        tvLot.setText(lotNum);
        rvLotHoldInfo.setLayoutManager(new LinearLayoutManager(Work_TrackOutActivity.this));
        rvLotHoldInfo.setItemAnimator(new DefaultItemAnimator());
        rvLotHoldInfo.addItemDecoration(new DividerItemDecoration(Work_TrackOutActivity.this, 1));
        trackOutHoldListAdapter = new TrackOutHoldListAdapter(null);
        trackOutHoldListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        trackOutHoldListAdapter.isFirstOnly(true);
        rvLotHoldInfo.setAdapter(trackOutHoldListAdapter);
    }

    private void initListTitle() {
        listTv1.setText("暂停代码");
        listTv2.setText("暂停原因");
        listTv3.setText("暂停备注");
        listTv4.setText("暂停责任组");
        listTv4.setVisibility(View.VISIBLE);
        listTv3.setVisibility(View.VISIBLE);
        listTv2.setVisibility(View.VISIBLE);
        listTv1.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initData() {

        JsonObject map = new JsonObject();
        map.addProperty("actionType", "Check");
        map.addProperty("lotId", lotNum);
        NetUtils.requestDara("HIK.PdaTrackOutLoad", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<TrackOutLoadBean>>(
                new TypeToken<BaseResponseBean<TrackOutLoadBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<TrackOutLoadBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    trackOutLoadBean = response.body().getBody();
                    tvMainWork.setText(trackOutLoadBean.getMainEquipments());
                    tvItemWork.setText(trackOutLoadBean.getSubEquipments());
                    tvCurrentStep.setText(trackOutLoadBean.getCurrentStepDesc());
                    tvLotNum.setText(String.valueOf(trackOutLoadBean.getMainQty()));
                    tvPartDesc.setText(trackOutLoadBean.getPartDesc());
                    tvPartName.setText(trackOutLoadBean.getPartName());
                    edcState = trackOutLoadBean.getEdcState();
                    trackOutHoldListAdapter.setNewData(trackOutLoadBean.getxPdaLotHolds());//暂停信息
                    if (edcState.equals("NONE") || edcState.equals("N")) {
                        bottomTv1.setEnabled(false);
                        bottomTv1.setBackgroundColor(getResources().getColor(R.color.free_ui_blue_sky_text_gray));
                        bottomTv2.setEnabled(true);
                        bottomTv2.setBackground(getResources().getDrawable(R.drawable.free_ui_glory_selector_click_no_corner_background));
                    } else {
                        bottomTv1.setEnabled(true);
                        bottomTv1.setBackground(getResources().getDrawable(R.drawable.free_ui_glory_selector_click_no_corner_background));
                        bottomTv2.setEnabled(false);
                        bottomTv2.setBackgroundColor(getResources().getColor(R.color.free_ui_blue_sky_text_gray));
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<TrackOutLoadBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaTrackOutLoad");

            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("待出站");
        freeUI_titleView.setRightListener("分片作业", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("lotID",lotNum);
                FreeApi_Utils.skipWithExtraIntent("header", bundle, Work_TrackOutActivity.this, Lot_ToArrayActivity.class);
            }
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick({R.id.bottom_tv1, R.id.bottom_tv2, R.id.bottom_tv3, R.id.bottom_tv4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bottom_tv1:
                if (actionControl("Work_SPCDataCollectActivity", "PDAWorkFragment", "无SPC数据采集权限"))
                    return;
                Bundle bundle = new Bundle();
                bundle.putString("lot", lotNum);
                bundle.putString("paramsName", trackOutLoadBean.getParameterSet());
                bundle.putString("mainDevice", trackOutLoadBean.getMainEquipments());
                bundle.putString("partName",trackOutLoadBean.getPartName());
                bundle.putString("partDesc",trackOutLoadBean.getPartDesc());
                bundle.putString("itemDevice", trackOutLoadBean.getSubEquipments() == null ? "" : trackOutLoadBean.getSubEquipments());
                FreeApi_Utils.skipWithExtraIntent("header", bundle, Work_TrackOutActivity.this, Work_SPCDataCollectActivity.class);
                break;
            case R.id.bottom_tv2:
                initPermission("TrackOut", this, new ActionControlListener() {
                    @Override
                    public void onControlChangeListener() {
                        submit("Comment", "HIK.PdaTrackOut");
                    }
                });

                break;
            case R.id.bottom_tv3:
                initPermission("TrackExit", this, new ActionControlListener() {
                    @Override
                    public void onControlChangeListener() {
                        String remarks = etRemarks.getText().toString().trim();
                        if (FreeApi_StringUtils.isEmpty(remarks)) {
                            showToast("请输入备注!", false);
                            return;
                        }
                        submit("AbortLot", "HIK.PdaTrackAbort");
                    }
                });
                break;
            case R.id.bottom_tv4:
                initPermission("Scrap", this, new ActionControlListener() {
                    @Override
                    public void onControlChangeListener() {
                        if (actionControl("Lot_LotScrapActivity", "PDALotFragment", "无批次报废权限"))
                            return;
                        Bundle bundle1 = new Bundle();
                        bundle1.putString("lot", lotNum);
                        FreeApi_Utils.skipWithExtraIntent("header", bundle1, Work_TrackOutActivity.this, Lot_LotScrapActivity.class);
                    }
                });
                break;
        }
    }


    private void submit(String submitType, String messageName) {
        DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, new DialogUtils.signCallback() {
            @Override
            public void onSignSuccess() {
                JsonObject map = new JsonObject();
                map.addProperty("lotId", lotNum);
                map.addProperty("actionType", "Check");
                map.addProperty("mainEquipmentId", trackOutLoadBean.getMainEquipments());
                map.addProperty("xGrapHiteBoats", "");
                map.addProperty("comment", etRemarks.getText().toString().trim());
                map.addProperty("operationType", submitType);

                NetUtils.requestDara(messageName, this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<TrackOutSubmitBean>>(
                        new TypeToken<BaseResponseBean<TrackOutSubmitBean>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<TrackOutSubmitBean>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                            if (submitType.equals("Comment")) {
                                String nextStepName = response.body().getBody().getNextStepDesc();
                                if (FreeApi_StringUtils.isEmpty(nextStepName)) {
                                    showConfirmDialog(Work_TrackOutActivity.this, "出站成功，没有下一道工序");
                                } else {
                                    showConfirmDialog(Work_TrackOutActivity.this, "出站成功，下一道工序是:" + nextStepName);
                                }

                            } else {
                                showConfirmDialog(Work_TrackOutActivity.this, "退站成功");
                            }

                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<TrackOutSubmitBean>> response) {
                        super.onError(response);
                        loadError(response.getException(), messageName);

                    }
                });
            }
        }, getSupportFragmentManager());

    }

}
